//
//  JSMDetailsViewController.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMDetailsViewController.h"
#import "JSMDetailsPictureViewController.h"
#import "JSMTouringDetailModel.h"
#import "JSMDetailsCell.h"

@interface JSMDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)BaseTableView *tableView;
@property(nonatomic, strong)NSMutableArray *detailArr;
@property(nonatomic, strong)BaseImageView *imageView;
@property(nonatomic, strong)NSString *detailURL;
@end

@implementation JSMDetailsViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"<<" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    [self createRightButton];
    self.title = self.str_title;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    
    [self createDate];
}

- (void)createRightButton {
    BaseButton *searchButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    searchButton.frame = CGRectMake(0, 0, 40, 40);
    
    if ([SaveTool isHaveModelInPlist:self.detailModel type:SaveTouringModel]) {
        [searchButton setImage:[UIImage imageNamed:SAVE_NOTHAVE_IMAGE] forState:UIControlStateNormal];
    }else{
        [searchButton setImage:[UIImage imageNamed:SAVE_ISHAVE_IMAGE] forState:UIControlStateNormal];
    }
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];
    [searchButton addTarget:self action:@selector(saveButton:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 解析数据
- (void)createDate{
    self.detailURL = [NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v2/travelog/%@?&api_version=1&app_version=4.3.3&device_type=2&travelog_id=%@", self.detailModel.touringId, self.detailModel.touringId];
    [AFNetworkingTool getUrl:self.detailURL hudSuperView:self.view result:JSON success:^(id result) {
        NSMutableDictionary *dic = result;
        self.detailArr = [JSMTouringDetailModel baseModelByArr:dic[@"log"][@"posts"]];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
        
    } ];
}

#pragma mark - 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.detailArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"reuse";
    JSMDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[JSMDetailsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[[[self.detailArr[indexPath.row] picturesMarr]lastObject] myPicture]] placeholderImage:[UIImage imageNamed:@"placeHolderImage"]];
    cell.descriptionLabel.text = [self.detailArr[indexPath.row] touringdescription];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
    CGRect rect = [[self.detailArr[indexPath.row] touringdescription]boundingRectWithSize:CGSizeMake(SCREENWIDTH - 10 ,0)  options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return 300 + rect.size.height + 20;
}

#pragma mark - 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    JSMDetailsPictureViewController *pictiureVC = [[JSMDetailsPictureViewController alloc]init];
    pictiureVC.num = (NSInteger)indexPath.row;
    pictiureVC.strURL = self.detailURL;
    pictiureVC.titleStr = [self.detailModel name];;
    pictiureVC.navigationItem.hidesBackButton = YES;
    [self.navigationController pushViewController:pictiureVC  animated:YES];
   }


#pragma mark 收藏
- (void)saveButton:(BaseButton *)button{
    NSString *message;
    if ([button.imageView.image isEqual:[UIImage imageNamed:SAVE_ISHAVE_IMAGE]]) {
        [SaveTool cancelSaveModelInPlist:self.detailModel type:SaveTouringModel];
        [button setImage:[UIImage imageNamed:SAVE_NOTHAVE_IMAGE] forState:UIControlStateNormal];
        message = @"取消收藏";
    }else{
        [SaveTool saveModelToPlist:self.detailModel type:SaveTouringModel];
        [button setImage:[UIImage imageNamed:SAVE_ISHAVE_IMAGE] forState:UIControlStateNormal];
        message = @"已收藏";
    }
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    UIAlertController *alertControler = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alertControler animated:YES completion:^{
    }];
    [self dismissViewControllerAnimated:YES completion:^{
    }];


}

#pragma mark - 懒加载
- (BaseTableView *)tableView{
    if (!_tableView) {
        _tableView = [[BaseTableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
